/**
 *  @邢玮 UI层提供系统对话
 * 基于antv提供多种形式提示机制，如，Drawer，Message,Modal,Notification ..
 * 如：app.message('浏览器版本不匹配,...')
 * */
import Vue from 'vue'
import { message, notification, Drawer, Modal, Icon } from 'ant-design-vue'
import _getCompByTagName from './util'
Vue.use(Modal)
class PopBox {
  constructor(opt) {
    notification.config({
      getContainer: () => document.querySelector('#app')
    })
    this.config = {
      type: 'success', //类型：success/error/info/warn
      title: '', //标题
      content: '', //内容
      duration: 3, //duration后弹出框关闭
      top: '24px', //距顶部距离
      onClose: function () {}, //关闭时的回调
      open: true
    }
    this.option = Object.assign(this.config, opt)
    this.config.open && this.open()
  }
  open() {}
  destroy() {}
}

class Msg extends PopBox {
  constructor(opt) {
    let option = {}
    option = Object.assign(option, opt)
    super(option)
    message.config({
      getContainer: () => document.querySelector('#app')
    })
  }
  open(params){
    this.option = Object.assign(this.option, params)
    message[this.option.type](this.option.content, this.option.duration, this.option.top, this.option.onClose)
  }
  destroy(){
    message.destroy()
  }
}

class Notice extends PopBox {
    constructor(opt) {
      var option = {
        key: '',
        message: '',
        description: '',
        placement: 'topRight',
        onClick: function(){}
      }
      option=Object.assign(option,opt)
      super(option);
    }
    open(params) {
        this.option = Object.assign(this.option, params)
        this.option.message = this.option.title
        this.option.description = this.option.content
        this.option.class = `pop-${this.option.type}`
        notification.open(this.option)
    }
    close(key){
        notification.close(key)
    }
    destroy(){
        notification.destroy()
    }

}

class Amodal {
    constructor(opt) {
      this.defaultOption = {
        type: 'success',
        componentsName: 'modalSimple',
        onClose: function(){}
      }
      this.option = {}
    }
    open(params){
      // if(this.option){
      //   this.destroy()
      //   setTimeout(() => {
      //     this.option = {}
      //     this.option = Object.assign({}, this.defaultOption, params)
      //     if(_getCompByTagName('modal')){
      //       let modal = _getCompByTagName('modal')[0]
      //       modal.open(this.option)
      //     }
      //   }, 1000)
      // }else {
      //   this.option = Object.assign({}, this.defaultOption, params)
      //   if(_getCompByTagName('modal')){
      //     let modal = _getCompByTagName('modal')[0]
      //     modal.open(this.option)
      //   }
      // }
      this.option = {}
      this.option = Object.assign({}, this.defaultOption, params)
      if(_getCompByTagName('modal')){
        let modal = _getCompByTagName('modal')[0]
        modal.open(this.option)
      }
    }
    destroy(params){
        let modal = _getCompByTagName('modal')[0]
        modal.destroy(this.option)
        this.option = {}
    }

}

class Confirm extends PopBox {
  constructor(opt) {
    let option = {
      okText: '确认',
      cancelText: '取消',
      onOK: function () {
        console.log('onOK')
      },
      onCancel: function () {
        console.log('onCancel')
      },
      class: 'anov-confirm'
    }
    option = Object.assign(option, opt)
    super(option)
  }
  open(params) {
    this.option = Object.assign(this.option, params)
    Modal.confirm(this.option)
  }
  destroy(obj) {
    obj.destroy()
  }
}

function msg(opt) {
  return new Msg(opt)
}

function notice(opt) {
  return new Notice(opt)
}
function confirm(opt) {
  return new Confirm(opt)
}
let modal = new Amodal()
export { msg, notice, modal, confirm }
